package leetcode;

import java.util.HashMap;

/**
 * @author ht
 * @create 2025-06-19 22:20
 * @desc
 **/
public class T454 {

    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int count = 0;
        HashMap<Integer, Integer> map = new HashMap<>();

        // 遍历前两个数组，记录数值和次数
        for (int i = 0; i < nums1.length; i++) {
            for (int j = 0; j < nums2.length; j++) {
                int num = nums1[i] + nums2[j];
                if(map.containsKey(num)) {
                    map.put(num, map.getOrDefault(num, 0) + 1);
                } else {
                    map.put(num, 1);
                }
            }
        }

        // 遍历后两个数据，找出加起来和等于0的
        for (int i = 0; i < nums3.length; i++) {
            for (int j = 0; j < nums4.length; j++) {
                int num = nums3[i] + nums4[j];
                if(map.containsKey(-num)) {
                    count += map.get(-num);
                }
            }
        }
        return count;
    }
}
